package com.air.admin.controller.imp;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.air.admin.controller.IAdminInfoManagerController;
import com.air.admin.mo.BasicAdminInfo;
import com.air.admin.service.IBasicAdminInfoService;
import com.air.admin.util.AdminAppConstant;
import com.air.admin.vo.AdminInfoQueryRequestVO;
import com.air.common.controller.imp.BaseControllerImp;
import com.air.common.mo.SystemConfig;
import com.air.common.service.ISystemConfigService;
import com.air.common.util.BasicAppConstant;
import com.air.common.util.BasicDBDictionary;
import com.air.common.util.CommonUtil;
import com.air.common.util.QueryCondition;
import com.air.common.util.QueryCondition4Update;
import com.air.common.util.QueryExpression;
import com.air.common.vo.PageResultListVO;
import com.air.common.vo.ResponseVO;

@Controller
@RequestMapping("/admin/adminInfoManager")
public class AdminInfoManagerControllerImp extends BaseControllerImp implements
		IAdminInfoManagerController {
	
	@Autowired
	IBasicAdminInfoService adminInfoService;
	
	@Autowired
	ISystemConfigService systemConfigService;
	
	@Override
	@RequestMapping("addAdmin")
	public ModelAndView addAdmin(HttpServletRequest request,
			HttpServletResponse response, BasicAdminInfo adminInfo)  throws Exception{
		
		adminInfo  = adminInfoService.registerAdmin(adminInfo);
		ResponseVO responseVO = new ResponseVO();
		responseVO.addObject("adminInfo", adminInfo);
		return parse2Mav(request,responseVO);
	}

	@Override
	@RequestMapping("listAdmin")
	public ModelAndView listAdmin(HttpServletRequest request,
			HttpServletResponse response, AdminInfoQueryRequestVO queryRequestVO)
			throws Exception {
		
		QueryCondition condition = new QueryCondition();
		if (queryRequestVO.getSort()!=null){
			condition.addOrderExpression(queryRequestVO.getSort(), queryRequestVO.getOrder());
		}
		PageResultListVO pageResult;
		if (queryRequestVO.getQueryMode()==null||queryRequestVO.getQueryMode().equalsIgnoreCase(AdminAppConstant._QUERY_MODE_SIMPLE)){
			pageResult = adminInfoService.queryInSimpleMode(condition, queryRequestVO.getPage()-1, queryRequestVO.getRows(), queryRequestVO.getSimpleQueryString());
		}else{
			pageResult = adminInfoService.queryInAdvanceMode(condition, queryRequestVO.getPage()-1, queryRequestVO.getRows(), queryRequestVO);
		}
		
		ModelAndView mav = new ModelAndView();
		mav.addObject(BasicAppConstant._KEY_RESPONSE_VO,JSONObject.fromObject(pageResult).toString());
		mav.setViewName(BasicAppConstant._VIEW_COMMON_JSON);
		return mav;
	}

	@Override
	@RequestMapping("removeAdmins")
	public ModelAndView removeAdmins(HttpServletRequest request,
			HttpServletResponse response, String adminList) {
		
		
		List<String> adminIdList = Arrays.asList(adminList.split(","));
		
		QueryCondition condition = new QueryCondition();
		condition.addQueryCondition("id", adminIdList, QueryExpression.IN);
		this.adminInfoService.deleteByCondition(condition);
		
		ResponseVO responseVO = new ResponseVO();
		
		return parse2Mav(request,responseVO);
	}
	
	@Override
	@RequestMapping("updateAdmin")
	public ModelAndView updateAdmin(HttpServletRequest request,
			HttpServletResponse response, BasicAdminInfo adminInfo) {
		
		BasicAdminInfo oldAdminInfo = adminInfoService.findById(adminInfo.getId());
		
		oldAdminInfo.setRoles(adminInfo.getRoles());
		adminInfoService.updateById(oldAdminInfo);
		ResponseVO responseVO = new ResponseVO();
		return parse2Mav(request, responseVO);
	}

	@Override
	@RequestMapping("activateAdmins")
	public ModelAndView activateAdmins(HttpServletRequest request,
			HttpServletResponse response, String adminList) {
		
		List<String> adminIdList = Arrays.asList(adminList.split(","));
		QueryCondition4Update condition4Update = new QueryCondition4Update();
		condition4Update.addUpdateFieldExpression("status", BasicDBDictionary._common_status_available);
		condition4Update.addQueryCondition("id", adminIdList, QueryExpression.IN);
		
		this.adminInfoService.updateByCondition(condition4Update);
		
		ResponseVO responseVO = new ResponseVO();
		
		return parse2Mav(request, responseVO);
	}
	
	@Override
	@RequestMapping("blockAdmins")
	public ModelAndView blockAdmins(HttpServletRequest request,
			HttpServletResponse response, String adminList) {
		
		
		List<String> adminIdList = Arrays.asList(adminList.split(","));
		QueryCondition4Update condition4Update = new QueryCondition4Update();
		condition4Update.addUpdateFieldExpression("status", BasicDBDictionary._common_status_not_available);
		condition4Update.addQueryCondition("id", adminIdList, QueryExpression.IN);
		
		this.adminInfoService.updateByCondition(condition4Update);
		
		ResponseVO responseVO = new ResponseVO();
		
		return parse2Mav(request, responseVO);
		
	}

	@Override
	@RequestMapping("resetPassword")
	public ModelAndView resetPassword(HttpServletRequest request,
			HttpServletResponse response, String adminList) throws Exception{
		
		
		SystemConfig defaultAdminPwdConf = systemConfigService.findConfigByKey(BasicAppConstant._SYSCONF_KEY_ADMIN_PASSWORD);
		String defaultPwd = defaultAdminPwdConf.getValue();
		
		List<String> adminIdList = Arrays.asList(adminList.split(","));
		QueryCondition4Update condition4Update = new QueryCondition4Update();
		condition4Update.addUpdateFieldExpression("password", CommonUtil.getMD5Str(defaultPwd));
		condition4Update.addQueryCondition("id", adminIdList, QueryExpression.IN);
		
		this.adminInfoService.updateByCondition(condition4Update);
		
		ResponseVO responseVO = new ResponseVO();
		responseVO.addObject("defaultPassword", defaultPwd);
		return parse2Mav(request, responseVO);
	}

}
